#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import time
from engine.common import *
if sys.platform == "darwin":
    adbpath = "adb "
else:
    adbpath = "/usr/bin/adb "
if (sys.argv.__len__())<4:
    print( "参数个数少了")
    exit(0)
handset = sys.argv[1]
currentHandsetSnumber = sys.argv[2]
command = sys.argv[3]
resultF = sys.argv[4]

filePath = sys.argv[5]

# 3.淘宝联盟好物圈页面点击复制文案
# ##复制文案后计算md5 保存到目录 判断 是否存在这个目录 不存在保存 存在 推出  保存xy坐标 给下一步用
# 之前界面:淘宝联盟好物圈页面
# 期待界面:淘宝联盟好物圈页面
# 事前计算:复制文案MinY
# 事前结果:posx,posy
# 动作执行:touchTap
# 事后计算:out*calcMd5AndSave*/tbk/alimama/*./temp/淘宝联盟好物圈页面.txt
# 事后结果:./temp/淘宝联盟好物圈页面.txt
# 出错状态转移码:None
#复制文案按钮



def tklMd5AndSave( handset,currentHandsetSnumber,resultF,filePath="./temp/result.txt",delay=5 ):
    ret = False
    print(handset)
    print(currentHandsetSnumber)
    setHandsetType(handset)
    setHandsetSno(currentHandsetSnumber)
    oldcontent = getAndSetMsgFromHandsetCliper("test",currentHandsetSnumber)

    content = "0.8復制整段🛒  [nr]------------------[nr]打开【T❥宝】[nr]💛¥"+getTkl(oldcontent)+"¢:/❤"

    if "测试剪贴板" in content:
        print("没有想要的内容，退下吧")
        ret = True
    else:
        dirMd5 = getMd5(oldcontent)
        if not os.path.exists(getHandsetFileSavePath(resultF)):
            os.makedirs(getHandsetFileSavePath(resultF))
        if not os.path.exists(getHandsetFileSavePath(resultF+dirMd5+"/")):
            #create it now and
            print(getHandsetFileSavePath(resultF+dirMd5+"/"))
            os.makedirs(getHandsetFileSavePath(resultF+dirMd5+"/"))
            with open(getHandsetFileSavePath(resultF  + "curmd5.txt"),'w') as f:
                print('wriete to file')
                f.write(dirMd5)
            with open(getHandsetFileSavePath(resultF+dirMd5+"/tkl.txt"),'w') as f:
                print('wriete to file')
                f.write(content)
        else:
            print('exist dir   '+dirMd5+'   now then  abort this task now--------')
            ret = True

    with open(getHandsetFileSavePath(filePath),'w')as f:
        if ret == True:
            f.write('True')
            return True  #TRUE 状态转移
        else:
            f.write('False')
            return False




def descSave( handset,currentHandsetSnumber,resultF,filePath="./temp/result.txt",delay=5 ):
    ret = False
    print(handset)
    print(currentHandsetSnumber)
    setHandsetType(handset)
    setHandsetSno(currentHandsetSnumber)
    content = getAndSetMsgFromHandsetCliper("test",currentHandsetSnumber)

    with open(getHandsetFileSavePath(resultF + "curmd5.txt")) as f:
        print('wriete to file')
        dirMd5 = f.read()

    with open(getHandsetFileSavePath(resultF + dirMd5 + "/desc.txt"), 'w') as f:
        print('wriete to file')
        f.write(content)
        ret = True

        # adb pull files to this dir and save
        # 联盟图片目录在  adb pull /storage/emulated/0/Pictures/淘宝联盟 temp pull到temp目录到 淘宝联盟
    AdbPullFromHandset("/sdcard/tencent/MicroMsg/WeiXin  " + getHandsetFileSavePath(""), currentHandsetSnumber)
    AdbPullFromHandset("/storage/emulated/0/Pictures/WeiXin  " + getHandsetFileSavePath(""), currentHandsetSnumber)
    # AdbShellRmFromHandset("/storage/emulated/0/Pictures/淘宝联盟/*.*",currentHandsetSnumber)

    if os.path.exists(getHandsetFileSavePath("WeiXin")):
        print(getHandsetFileSavePath("WeiXin") + getHandsetFileSavePath(resultF) + dirMd5 + "/")
        cmd = "cp " + getHandsetFileSavePath("WeiXin") + "/* " + getHandsetFileSavePath(resultF) + dirMd5 + "/"
        os.system(cmd)
        #print("9sec will del files ")
        #time.sleep(9)
        cmd = "rm -rf " + getHandsetFileSavePath("WeiXin")
        os.system(cmd)



    with open(getHandsetFileSavePath(filePath),'w')as f:
        if ret == True:
            f.write('True')
            return True  #TRUE 状态转移
        else:
            f.write('False')
            return False




if command =="tklMd5AndSave".strip():
    tklMd5AndSave(handset,currentHandsetSnumber,resultF,filePath)
if command =="descSave".strip():
    descSave(handset,currentHandsetSnumber,resultF,filePath)
